package com.topboya.linker.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.topboya.linker.AppConst;
import com.topboya.linker.R;
import com.topboya.linker.model.response.ProductInforListReponse;
import com.topboya.linker.model.response.SelectFileResponse;
import com.topboya.linker.ui.activity.FileDetailWebActivity;
import com.topboya.linker.utils.LogUtils;
import com.topboya.linker.utils.UIUtils;

import java.util.ArrayList;

public class SelectFileAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final String TAG = "SelectFileAdapter:";
    private ArrayList<SelectFileResponse.FileList> mFileList;
    private Context mContext;
    private String imgbaseurl = "https://otc.topboya.com";

    public SelectFileAdapter(Context mContext,ArrayList<SelectFileResponse.FileList> mFileList) {

        if (mFileList == null) {
            throw new IllegalArgumentException("model Data must not be null");
        }
        this.mFileList = mFileList;
        this.mContext = mContext;
    }




    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {

        View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.select_file_item, viewGroup, false);
        return new SelectFileAdapter.ListItemViewHolder(itemView);
    }

    //绑定界面，设置监听
    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int i) {

        LogUtils.v(TAG + "mFileList----"+mFileList.size());
        //设置条目状态
        ((SelectFileAdapter.ListItemViewHolder) holder).tv_file_name.setText(mFileList.get(i).getFileName());
        LogUtils.v(TAG + "mFileList.get(i).getFileSuffix()):"+mFileList.get(i).getFileSuffix());
        Glide.with(mContext).load(imgbaseurl+mFileList.get(i).getStoreMinPath()).placeholder(placeholder(mFileList.get(i).getFileSuffix())).into(((ListItemViewHolder) holder).img_dispaly);
        ((ListItemViewHolder) holder).img_filetype.setBackgroundResource(getFileType(mFileList.get(i).getFileSuffix()));

        if (null !=AppConst.FILE_SELECTED_MAP.get(mFileList.get(i).getId())){

            ((ListItemViewHolder) holder).img_checked.setBackgroundResource(R.drawable.selected_depart_bg);

        }else {

            ((ListItemViewHolder) holder).img_checked.setBackgroundResource(R.drawable.no_select_depart_bg);
        }

        //条目view的监听
        ((SelectFileAdapter.ListItemViewHolder) holder).itemView.setOnClickListener(v -> {
//            获取资料id，加载web页面
            Intent intent = new Intent(mContext,FileDetailWebActivity.class);
            intent.putExtra("fifeID",mFileList.get(i).getId());
            mContext.startActivity(intent);

        });
        ((ListItemViewHolder) holder).img_checked.setOnClickListener(v1 -> {

            if (null !=itemClickListener){

                itemClickListener.onClick(holder.itemView ,i);
            }
            if (null==AppConst.FILE_SELECTED_MAP.get(mFileList.get(i).getId())) {

                ((ListItemViewHolder) holder).img_checked.setBackgroundResource(R.drawable.no_select_depart_bg);

            } else {

                ((ListItemViewHolder) holder).img_checked.setBackgroundResource(R.drawable.selected_depart_bg);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mFileList == null ? 0 : mFileList.size();
    }

    public class ListItemViewHolder extends RecyclerView.ViewHolder{

        //ViewHolder
        ImageView  img_filetype;
        ImageView img_dispaly;
        ImageView img_checked;
        TextView tv_file_name;

        ListItemViewHolder(View view) {
            super(view);
            this.img_filetype = view.findViewById(R.id.img_filetype);
            this.img_dispaly = view.findViewById(R.id.img_dispaly);
            this.img_checked = view.findViewById(R.id.img_checked);
            this.tv_file_name = view.findViewById(R.id.tv_file_name);

        }

    }

    private SelectFileAdapter.OnItemClickListener itemClickListener;

    public void setOnItemClickListener(SelectFileAdapter.OnItemClickListener itemClickListener){

        this.itemClickListener = itemClickListener;
    }

    public interface  OnItemClickListener{

        void onClick(View v, int position);
    }

    public int getFileType(String fileSuffix){
        
        int drawableID;
        if (null ==fileSuffix ||"".equals(fileSuffix)){

            drawableID = R.drawable.picture;

        }else if (fileSuffix.equals("jpg")||fileSuffix.equals("jpeg")) {

             drawableID = R.drawable.picture;
         }else if (fileSuffix.equals("ppt")){

             drawableID = R.drawable.ppt;

         }else if (fileSuffix.equals("pdf")) {

             drawableID = R.drawable.pdf;
         }else if (fileSuffix.equals("mp4")) {

             drawableID = R.drawable.video;
         }else {

             drawableID = R.drawable.ppt;
         }

         return drawableID;
    }
//    加载默认图片
    public int placeholder(String fileSuffix){
        int drawableID;
        if (null ==fileSuffix ||"".equals(fileSuffix)){

            drawableID = R.drawable.picture_default;

        }else if (fileSuffix.equals("jpg")||fileSuffix.equals("jpeg")) {

            drawableID = R.drawable.picture_default;
        }else if (fileSuffix.equals("ppt")){

            drawableID = R.drawable.ppt_default;

        }else if (fileSuffix.equals("pdf")) {

            drawableID = R.drawable.pdf_default;

        }else if (fileSuffix.equals("mp4")) {

            drawableID = R.drawable.video_default;
        }else {

            drawableID = R.drawable.picture_default;
        }
        return drawableID;
    }
}
